package com.dgut.service.serviceImpl;

import com.dgut.dao.UserDao;
import com.dgut.pojo.User;
import com.dgut.service.UserService;
import com.dgut.util.PageResult;
import com.dgut.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "userCache")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public void saveUser(User user) {
        userDao.save(user);
    }

    @Override
    public void saveUsers(List<User> users) {
        for(User u:users){
            userDao.save(u);
        }
    }

    @Override
    public PageResult findAll(PageRequest pageRequest) {
        return PageUtils.getPageResult(userDao.findAll(pageRequest));
    }

    @Override
    public void deleteUserById(Long id) {
        userDao.deleteById(id);
    }

    @Override
    @Cacheable(key = "#root.args[0]")
    public User findUserById(Long id) {
        return userDao.findUserById(id);
    }

    @Override
    @Cacheable(key = "#root.args[0]")
    public User findUserByName(String name) {
        return userDao.findByName(name);
    }
    @Override
    public void updateUser(User user) {
        userDao.save(user);
    }

    @Override
    public PageResult findUserByNoRecord(PageRequest pageRequest) {
        return PageUtils.getPageResult(userDao.findUserByNoRecord(pageRequest));
    }

    @Override
    public PageResult findUserByOverTemperature(PageRequest pageRequest) {
        return PageUtils.getPageResult(userDao.findUserByOverTemperature(pageRequest));
    }

    @Override
    public PageResult findUserByOverJourney(PageRequest pageRequest) {
        return PageUtils.getPageResult(userDao.findUserByOverJourney(pageRequest));
    }

    @Override
    public PageResult findUserByOverContact(PageRequest pageRequest) {
        return PageUtils.getPageResult(userDao.findUserByOverContact(pageRequest));
    }

}